using System;
using System.Collections.Generic;
using System.Text;
using Entity;

namespace eShop.Business
{
    public class NewsletterGroup
    {
        private int _groupId;
        public int GroupId
        {
            get { return this._groupId; }
            set { this._groupId = value; }
        }

        private string _groupName;
        public string GroupName
        {
            get { return this._groupName; }
            set { this._groupName = value; }
        }

        private bool _isActive;
        public bool IsActive
        {
            get { return this._isActive; }
            set { this._isActive = value; }
        }

        public NewsletterGroup()
        {
            this._groupId = 0;
            this._groupName = null;
            this._isActive = false;
        }

        public NewsletterGroup(int groupId, string groupName, bool isActive)
        {
            this._groupId = groupId;
            this._groupName = groupName;
            this._isActive = isActive;
        }

        public NewsletterGroup(TblNewsletterGroup group)
        {
            this._groupId = group.NewsletterGroupId;
            this._groupName = group.NewsletterGroupName;
            this._isActive = group.IsActive;
        }

        private static List<NewsletterGroup> LoadFromList(List<TblNewsletterGroup> data)
        {
            List<NewsletterGroup> result = new List<NewsletterGroup>();

            for (int i = 0; i < data.Count; i++)
            {
                result.Add(new NewsletterGroup(data[i]));
            }

            return result;
        }

        public static List<NewsletterGroup> GetAllActiveGroups()
        {
            List<TblNewsletterGroup> data = TblNewsletterGroupExtended.GetAllActiveNewsletterGroup();
            List<NewsletterGroup> groups = LoadFromList(data);
            return groups;
        }

        
        public static NewsletterGroup Load(int NewsletterGroupId)
        {
            TblNewsletterGroup group = TblNewsletterGroup.GetTblNewsletterGroupByNewsletterGroupId(NewsletterGroupId);
            return new NewsletterGroup(group);
        }

        public static void DeleteSubscriberGroups(List<int> idList)
        {
            TblNewsletterGroup.DeleteTblNewsletterGroupList(idList);
        }

        public void Update()
        {
            TblNewsletterGroup group = new TblNewsletterGroup(GroupId, GroupName, IsActive);
            group.Save();
            GroupId = group.NewsletterGroupId;
        }

        public static int GetSubscriberGroupCount()
        {
            return TblNewsletterGroup.GetAllTblNewsletterGroupPagedCount("", 0, 0);
        }

        public static List<NewsletterGroup> GetPagedSubscriberGroups(int CurrentPageIndex, int PageSize, string SortBy)
        {
            int startRow = (CurrentPageIndex - 1) * PageSize;
            List<TblNewsletterGroup> data = TblNewsletterGroup.GetAllTblNewsletterGroupPaged(SortBy, startRow, PageSize);
            List<NewsletterGroup> groups = LoadFromList(data);
            return groups;
        }

        public static int GetSubscriberGroupCountByGroupName(string SubscriberGroupName)
        {
            return TblNewsletterGroupExtended.GetAllTblNewsletterGroupCountByGroupName(SubscriberGroupName);
        }

        public static List<NewsletterGroup> GetPagedSubscriberGroupsByGroupName(int CurrentPageIndex, int PageSize, string SortBy, string SubscriberGroupName)
        {
            int startRow = (CurrentPageIndex - 1) * PageSize;
            List<TblNewsletterGroup> data = TblNewsletterGroupExtended.GetAllTblNewsletterGroupByGroupName(SortBy, startRow, PageSize, SubscriberGroupName);
            List<NewsletterGroup> groups = LoadFromList(data);
            return groups;
        }
    }
}
